using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Net;
using System.Web;
using System.Web.Security;
using cms.libs.Utility;

namespace cms.libs
{
    /// <summary>
    /// Summary description for Config
    /// </summary>
    public sealed class Config
    {
        private static readonly Config instance = new Config();
        private string _UrlRoot;
        private string _SiteUrl;
        private string _SQLConn;
        private string _SQLConn_search;
        private string _SQLConn_common;
        private string _CachePool;

        public static string SQLConn
        {
            get
            {
                return instance._SQLConn;
            }
        }

        private string _mediaUrl;

        public static string mediaUrl
        {
            get
            {
                string sRet = instance._mediaUrl;
                if (!sRet.EndsWith("/"))
                    sRet += "/";
                return sRet;
            }
        }

        private string _mediaPath;

        public static string mediaPath
        {
            get
            {
                string sRet = instance._mediaPath;
                if (!sRet.EndsWith("/"))
                    sRet += "/";
                return sRet;
            }
        }

        private string _SSODomain;

        public static string SSODomain
        {
            get
            {
                return instance._SSODomain;
            }
        }


        public static string UrlRoot
        {
            get
            {
                return instance._UrlRoot;
            }
        }

        public static string SiteUrl
        {
            get
            {
                return instance._SiteUrl;
            }
        }

        public static string CachePool
        {
            get
            {
                return instance._CachePool;
            }
        }

        public static int AccountID
        {
            get
            {
                try
                {
                    var current = new SessionUtility().GetValue(SessionUtility.SessionAccount);

                    if (current == null) return 0;
                    var arrCurrent = current.ToString().Split('|');
                    if (arrCurrent.Length < 2) return 0;
                    var accountID = Convert.ToInt32(arrCurrent[0]);
                    return accountID;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        private Config()
        {
            _SQLConn = getConnStr("SQLConn");
            _mediaUrl = getAppStr("mediaUrl");
            _mediaPath = getAppStr("mediaPath");
            _UrlRoot = getAppStr("rootPath");
            _SiteUrl = getAppStr("SITE_URL");
        }

        private string getConnStr(string strConnName)
        {
            return ConfigurationManager.ConnectionStrings[strConnName].ConnectionString;
//            if (ConfigurationManager.ConnectionStrings[Name] == null)
//                return "";
//            if (ConfigurationManager.ConnectionStrings[Name].ConnectionString == "")
//                return "";
//            RijndaelEnhanced rijndaelKey = new RijndaelEnhanced("portal.web", "@1B2c3D4e5F6g7H8");
//            return rijndaelKey.Decrypt(ConfigurationManager.ConnectionStrings[Name].ConnectionString);
        }

        private string getAppStr(string Name)
        {
            return ConfigurationSettings.AppSettings[Name] == null ? "" : ConfigurationSettings.AppSettings[Name].ToString();
        }

        private static Config Instance
        {
            get { return instance; }
        }
    }
}